//
//  DicData.m
//  bh
//
//  Created by lanou on 16/1/4.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "DicData.h"
#import "FMDatabase.h"
static FMDatabase *_db;

@implementation DicData
+ (void)initialize {
    
    // 1、打开数据库
    NSString *strPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingString:@"shops.sqlite"];
    
    _db= [FMDatabase databaseWithPath:strPath];
    
    NSLog(@"%@", strPath);
    [_db open];
    
    // 2、 创表
    // executeQuery:  读取数据
    //  [self.db executeQuery:<#(NSString *), ...#>];
    
    // executeUpdate: 除查询数据以外的其他数据
    [_db executeUpdate:@"create table t_shop (id integer PRIMARY KEY, dic blob NOT NULL)"];
    
}
// 插入数据
+ (void)addshop:(NSDictionary *)dictionary {
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:dictionary];
    [_db executeUpdateWithFormat:@"insert into t_shop (dic) values(%@);", data];
}

// 查询数据
+ (NSMutableArray *)shops {
    FMResultSet *set = [_db executeQuery:@"select *from t_shop"];
    
    // 不断的往下取数据
    NSMutableArray *shops = [NSMutableArray array];
    while (set.next) {
        // 获取当前所指向的数据
  
        
         NSData *data = [set objectForColumnName:@"dic"];
        NSDictionary *dict = [NSKeyedUnarchiver unarchiveObjectWithData:data];
        [shops addObject:dict];
        
    }
    return shops;
}

// 删除数据
+ (void)deleteShops {
    [_db executeUpdateWithFormat:@"delete from t_shop"];
}

// 更改数据
+ (void)changeShops {
    
}

@end
